﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;

namespace GDI_AnalysisComponents
{
    //鼠标移动类
    class GMouseMove
    {
        public GMouseMove()
        {
        }

        //处理鼠标移动
        public string HandleGMouseMove(MouseEventArgs e, Control cl, GGraph ggraph,  MoveGComponent mgc, CreateGLine createGLine)
        {
            string s = null;
            cl.Cursor = System.Windows.Forms.Cursors.Default;
            Graphics g = cl.CreateGraphics();
            PointF  mousePt = new PointF(e.X, e.Y);
            ArrayList gcSavs = ggraph.GcSavs;
            ArrayList gcSels = ggraph.GcSels;
            MouseEventMode mem = ggraph.MEM;
            switch (mem)
            {
                case MouseEventMode.Normal:
                    if (isEnterHeadGUnit(gcSavs, mousePt) == true)
                        s=HandleEnterHeadGUnit(cl, gcSavs, mousePt);
                    if (isEnterSelRects(gcSels, mousePt) == true)
                        HandleEnterSelRects(cl, gcSels, mousePt);
                    break;
                //case MouseEventMode.LinkLine:  
                //createGLine.MouseMove(g, mousePt);
                //gsts.MEM = MouseEventMode.LinkLineMove;    
                //cl.Invalidate();
                //break;
                case MouseEventMode.LinkLineMove:
                    createGLine.MouseMove(g,mousePt);
                    break;
                case MouseEventMode.SelectComponent:
                    s=HandleMoveGComponent(cl, gcSels, mousePt, mgc);
                    cl.Invalidate();
                    ggraph.MEM = MouseEventMode.MoveComponent;
                    break;
                case MouseEventMode.MoveComponent:
                    s=HandleMoveGComponent(cl, gcSels, mousePt, mgc);
                    cl.Invalidate();                  
                    break;
                case MouseEventMode.TransformShape:
                    break;
                case MouseEventMode.AddComponent:
                    cl.Cursor = System.Windows.Forms.Cursors.Cross;
                    break;
            }
            return s;
        }

        //判断是否进入组件头
        public bool isEnterHeadGUnit(ArrayList  gcSavs, PointF mousePt)
        {
            bool ENTERHEADGUNIT=false ;
            for (int i = 0; i < gcSavs.Count; i++)
            {
                GComponent gc = (GComponent)(gcSavs[i]);
                RectangleF headGUnitRect = gc.GetGComponentHeadGUnitRect();
                if (headGUnitRect.Contains(mousePt))
                     ENTERHEADGUNIT=true;
            }
            return ENTERHEADGUNIT;
        }

        //处理进入组件头事件
        public string HandleEnterHeadGUnit(Control cl, ArrayList gcSavs, PointF mousePt)
        {
            string s = null;
            for (int i = 0; i < gcSavs.Count; i++)
            {
                GComponent gc = (GComponent)(gcSavs[i]);
                RectangleF headGUnitRect = gc.GetGComponentHeadGUnitRect();
                if (headGUnitRect.Contains(mousePt))
                {
                    //鼠标设置
                    cl.Cursor = System.Windows.Forms.Cursors.SizeAll;
                    s = "鼠标进入组件："+gc.ID+gc.Name;
                }
            }
            return s;
        }

        //判断是否进入选择组件的选择矩形
        public bool isEnterSelRects(ArrayList gcSels, PointF mousePt)
        {      
            bool ENTERSELRECTS = false;
            for (int i = 0; i < gcSels.Count; i++)
            {
                GComponent gc = (GComponent)gcSels[i];
                for (int j = 0; j < gc.SelRects.Count; j++)
                {
                    RectangleF rect = (RectangleF)(gc.SelRects[j]);
                    if (rect.Contains(mousePt))
                    {
                        ENTERSELRECTS = true;
                    }
                }
            }
            return ENTERSELRECTS;
        }

        //处理判断是否进入选择组件的选择矩形
        public void HandleEnterSelRects(Control cl, ArrayList gcSels, PointF mousePt)
        {
            cl.Cursor = System.Windows.Forms.Cursors.Default;

            for (int i = 0; i < gcSels.Count; i++)
            {
                GComponent gc = (GComponent)gcSels[i];
                for (int j = 0; j < gc.SelRects.Count; j++)
                {
                    RectangleF rect = (RectangleF)(gc.SelRects[j]);
                    if (rect.Contains(mousePt))
                    {
                        switch (j)
                        {
                            case 0:
                                cl.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
                                break;
                            case 1:
                                cl.Cursor = System.Windows.Forms.Cursors.SizeNESW;
                                break;
                            case 2:
                                cl.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
                                break;
                            case 3:
                                cl.Cursor = System.Windows.Forms.Cursors.SizeNESW;
                                break;
                        }
                    }
                }
            }
        }

        //处理移动组件
        public string  HandleMoveGComponent(Control cl, ArrayList gcSels, PointF mousePt, MoveGComponent mgc)
        {
            string s = null;
            Graphics g = cl.CreateGraphics();
            mgc.MouseMove(g, mousePt, gcSels);
            s="组件移动到："+mousePt.X+","+mousePt.Y+"！";
            return s;
        }
    }
}
